using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MOKA.TPM.Application.Enum;
using MOKA.TPM.Application.Service;
using MOKA.TPM.Entity;
using MOKA.TPM.Service.Dto;
using MOKA.TPM.Utility;
using MOKA.TPM.Utility.Const;
using SqlSugar;

namespace MOKA.TPM.Service;

/// <summary>
/// TPM非采库存业务逻辑控制器
/// </summary>
[ApiDescriptionSettings(TPMConst.GroupName, Name = "TPM_CheckupItemTPMCategory", Order = 305)]
public class CheckupItemTPMCategoryService : TPMBaseService, IDynamicApiController, ITransient
{
    private readonly Service_Equipment _equipmentService;
    private readonly Service_CheckupItemTPMCategory _checkupItemTPMCategoryService;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="rep"></param>
    /// <param name="userManager"></param>
    /// <param name="accessor"></param>
    /// <param name="equipmentStockService"></param>
    /// <param name="dicService"></param>
    public CheckupItemTPMCategoryService(UserManager userManager, IHttpContextAccessor accessor, ISqlSugarClient db, Service_CheckupItemTPMCategory checkupItemTPMCategoryService, Service_SysDictDetail dicService, Service_Equipment equipmentService)
                : base(userManager, dicService, accessor, db)
    {
        _checkupItemTPMCategoryService = checkupItemTPMCategoryService;
        _equipmentService = equipmentService;
    }

    /// <summary>
    /// 查询数据
    /// </summary>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "DoQuery"), HttpPost]
    [AllowAnonymous]
    public async Task<CommonResult> DoQueryAsync(GetCheckupItemTPMCategoryInput obj)
    {
        LogHelper.Write("查询库存:", obj.ToString());
        CommonResult result = new CommonResult();

        try
        {
            //先找出设备类型
            var CompanyCode = (await _dicService.GetDetailByItemValue("CompanyCode", "1525", TPMDeviceGroupEnum.EquipmentGroup)).Id;
            var TPMCategoryId = (await _equipmentService.GetOneByUniq(CompanyCode, "设备编码")).TPMCategoryId;

            var equipmentStockEntities = await _checkupItemTPMCategoryService.GetList(
                new GetCheckupItemTPMCategoryInput
                {
                    CheckupCycle = obj.CheckupCycle,
                    TPMCategoryId = TPMCategoryId
                });

            if (equipmentStockEntities != null && equipmentStockEntities.Count > 0)
            {
                result.Data = equipmentStockEntities;
                result.Result = true;
            }
        }
        catch (Exception ex)
        {
            result.Data = null;
            result.Result = false;
            result.Error = ex.Message.ToString();
        }
        return result;
    }
}